package com.offcn.sms.util;

import com.offcn.common.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SmsUtil {

    @Value("${sms.appcode}")
    private String appcode;

    @Value("${sms.tplId}")
    private String tplId;

    @Value("${sms.host}")
    private  String host;

    @Value("${sms.path}")
    private String path;

    @Value("${sms.method}")
    private String method;

    public HttpResponse sendSms(String phone, String code) {
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("param", "code:"+ code);
        querys.put("tpl_id", tplId);
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
